package com.tiantianyuegou.util;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by George on 2016/8/31.
 */
public class StringUtils {
    /**
     * 判断是否为空
     *
     * @param value
     * @param defaultValue
     * @return
     * @Description:
     */
    public static String checkIsNull(String value, String defaultValue) {
        if (TextUtils.isEmpty(value) || "null".equalsIgnoreCase(value))
            return defaultValue;
        return value;
    }

    /**
     * 获取string的长度
     *
     * @param text
     * @return
     * @Description:
     */
    public static int getStringLength(String text) {
        text = text.replaceAll("[^\\x00-\\xff]", "**");
        return text.length();
    }

    /**
     * 检查string长度
     *
     * @param text
     * @param length
     * @return
     * @Description:
     */
    public static boolean checkStringLength(String text, int length) {
        return getStringLength(text) <= length;
    }

    /**
     * 验证手机号码是否合格
     *
     * @return
     * @Description:
     */
    public static boolean checkPhone(String phone) {
        String regex = "^1[3|5|7|8|4|6|9|][0-9]{9}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    /**
     * 验证邮箱格式是否合格
     *
     * @param email
     * @return
     * @Description:
     */
    public static boolean checkEmail(String email) {
        String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    /**
     * 验证密码长度6-16位
     *
     * @param password
     * @return
     * @Description:
     */
    public static boolean checkPasswordLength(String password) {
        int length = password.length();
        if (length > 16 || length < 6) {
            return false;
        }
        return true;
    }

    /**
     * 格式化折扣
     *
     * @return
     * @Description:
     */
    public static String formatDiscount(float discount) {
        int tmp = (int) discount;
        if (discount - tmp != 0) {
            return new DecimalFormat(",##0.0").format(discount);
        } else {
            return new DecimalFormat("##0").format(discount);
        }
    }

    /**
     * 格式化金额
     *
     * @param money
     * @return
     */
    public static String formatMoney(float money) {
        return String.valueOf(new DecimalFormat(",##0.00").format(money));
    }

    public static String formatMoney2(float money) {
        return String.valueOf(new DecimalFormat("###0.00").format(money));
    }

    /**
     * 获取格式化后的金额字符串
     *
     * @param money
     *            金额
     * @return 格式化后的金额字符串
     */
    public static String getFormatMoney(double money) {
        NumberFormat nf = null;
        nf = new DecimalFormat(",##0.00");
        return String.valueOf(nf.format(money));
    }

    /**
     * 转换性别
     *
     * @param sex
     * @return
     * @Description:
     */
    public static String formatSex(String sex) {
        if (TextUtils.equals(sex, "1")) {
            return "男";
        } else if (TextUtils.equals(sex, "2")) {
            return "女";
        }
        return "";
    }

    /**
     * 格式化分数
     *
     * @param score
     * @return
     * @Description:
     */
    public static String formatScore(float score) {
        DecimalFormat decimalFormat=new DecimalFormat("##0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        return String.format("%s分", decimalFormat.format(score));
    }

    /**
     * 格式化销量
     *
     * @return
     * @Description:
     */
    public static String formatSales(int sales) {
        String value = String.valueOf(sales);
        if (sales > 1000) {
            value = String.valueOf("999+");
        }
        return String.format("/月销%s笔", value);
    }

    /**
     * 判断是否是http地址
     *
     * @param text
     * @return
     * @Description:
     */
    public static boolean checkIsHttpUrl(String text) {
        String regex = "^http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }

    /**
     * 格式化城市，去除市
     *
     * @return
     * @Description:
     */
    public static String formatCity(String city) {
        if (TextUtils.isEmpty(city))
            return "";
        return city.replaceAll("市", "");
    }

    /**
     * 格式化地址
     * @param addr
     * @return
     * @Description:
     */
    public static String formatAddress(String addr) {
        return addr.replaceAll("中国", "");
    }

    /**
     * 加密身份证号码
     * @param cardNum
     * @return
     * @Description:
     */
    public static String encodeCardNum(String cardNum) {
        if (TextUtils.isEmpty(cardNum)) {
            return cardNum;
        }
        return cardNum.replaceAll("(?<=\\d{1})\\d(?=\\d{1})", "*");
    }
}
